package com.qingshui.browser.ui.views;

import com.qingshui.browser.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class TabListLayout extends LinearLayout{
	private int maxHeight;
	private int currentHeight;
	private int maxCount;
	private OnItemClickedListener mClickedListener;
	private OnItemRemoveListener mRemoveListener;
	public TabListLayout(Context context){
		super(context);
	}
	public TabListLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		maxHeight=0;
		currentHeight=0;
		maxCount=4;
		///setTouchRect();
	}

	public boolean onTouchEvent(MotionEvent event){
		//Log.d("touch", "x"+event.getX()+" y:"+event.getY());
		return super.onTouchEvent(event);
	}
	
	public static interface OnItemClickedListener {
		/**
		 * 
		 * @param position Position of the tab that have been clicked.
		 */
		void onItemClicked(View view, int position);
	}
	
	public int getMaxCount(){
		return maxCount;
	}
	public static interface OnItemRemoveListener {
		/**
		 * 
		 * @param position Position of the tab that have been clicked.
		 */
		void onItemRemoveClicked(int position);
	}
	public void setTab(int index ,String title , int drawId){
		Drawable icon = getResources().getDrawable(drawId);
		setTab(index, title, icon);
	}
	public void setTab(int index, String title, Drawable icon){
		if(index>=0 && index<this.getChildCount()){
			LinearLayout child = (LinearLayout)this.getChildAt(index);
			if(child !=null){
				if(title!=null){
				TextView childText = (TextView)child.findViewById(R.id.TabText);
				
				childText.setText(title);
				}
				if(icon!=null){
					ImageView childImage = (ImageView)child.findViewById(R.id.TabImage);
					childImage.setImageDrawable(icon);
				}
			}
		}
	
	}

	public void addTab(int titleId,int drawId){
		Drawable icon = getResources().getDrawable(drawId);
		String title = getResources().getString(titleId);
		addTab(title,icon);
	}
	public void addTab(int titleId,Drawable icon){
		//Drawable icon = getResources().getDrawable(drawId);
		String title = getResources().getString(titleId);
		addTab(title,icon);
	}
	public void addTab(String title,int drawId){
		Drawable icon = getResources().getDrawable(drawId);
		addTab(title,icon);
	}
	/**
	 * @param title the tab's title
	 * @param icon the tab's icon
	 * */

	public void addTab(String title, Drawable icon){
		if(this.getCount()>maxCount){
			return ;
		}
		final LayoutInflater inflater = LayoutInflater.from(getContext());
		View layout = (View) inflater.inflate(R.layout.tab_list_item, this, false);
		TextView text = (TextView)layout.findViewById(R.id.TabText);
		ImageView image=(ImageView)layout.findViewById(R.id.TabImage);
		ImageButton button=(ImageButton)layout.findViewById(R.id.RemoveTab);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int index =TabListLayout.this.indexOfChild((View)v.getParent());
				removeTab(index);
			}
		});
		image.setImageDrawable(icon);
		text.setText(title);
		layout.setClickable(true);
		layout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mClickedListener!=null){
					mClickedListener.onItemClicked(v, TabListLayout.this.indexOfChild(v));
				}
			}
		});
		this.addView(layout);
		this.requestLayout();
		//Log.d("tablist",""+mTabs.indexOfChild(linearLayout)+" sum:"+mTabs.getChildCount());
		
	}
	
	public int getCount(){
		return this.getChildCount();
	}
	
	/**
	 * 
	 * 
	 * */
	protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int measureHeight = this.getMeasuredHeight();
		if(maxHeight<=0){
			
		}else if(measureHeight>maxHeight){
			super.onMeasure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY));
		}
		measureHeight = this.getMeasuredHeight();
		//this.setTouchRect();
	}
	
	/**
	 * @author wjc
	 * 	@param index remove the one in the index
	 * */
	public void removeTab(int index){
		if(index>=0&&index<this.getChildCount()){
			this.removeViewAt(index);
			if(mRemoveListener != null){
			mRemoveListener.onItemRemoveClicked( index);
			}
		}
	}
	
	public void setOnItemClickedListener(OnItemClickedListener listener){
		this.mClickedListener=listener;
		
	}
	public void setOnItemRemoveListener(OnItemRemoveListener listener){
		this.mRemoveListener=listener;
	}
	public int getMaxHeight() {
		return maxHeight;
	}
	public void setMaxHeight(int maxHeight) {
		this.maxHeight = maxHeight;
	}
}
